package javaweb.oa.web.action;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javaweb.oa.web.action.Utils.DBUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DeptEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");

        String deptno = request.getParameter("deptno");
        String contextPath = request.getContextPath();

out.print("       <!doctype html>");
out.print("<html>");
out.print("	<head>");
out.print("		<meta charset='UTF-8'/>");
out.print("		<title>修改页面</title>");
out.print("	</head>");
out.print("	<body>");
out.print("		<h1 align='center'>修改部门</h1>");
out.print("		<hr>");
out.print("		<form action='"+contextPath+"/dept/save' method='post'>");

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept where deptno=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            rs = ps.executeQuery();
            while(rs.next()){
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
              out.print("  部门编号<input type='text' name='deptno' value="+deptno+" readonly><br>");
              out.print("          部门名称<input type='text' name='dname' value="+dname+"><br>");
              out.print("          部门地址<input type='text' name='loc' value="+loc+"><br><br>");
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
out.print("        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
out.print("			<input type='submit' value='保存' />");
out.print("		</form>");
out.print("	</body>");
out.print("</html>");

    }
}
